<script setup lang="ts">
import { searchConfig } from './config/search-config';
import { tableConfig } from './config/table-config';
// import { EServiceType } from '@/api/common/type';
import { doGetCustomerInformationList } from '@/api/base/customerInformation';
import { generateNameToLevel } from '@/config/constant';
import type { CustomerInformationListInfo } from '@/api/base/customerInformation/model/type';

defineOptions({
    name: 'CustomerInformation'
});

const props = defineProps(['visible', 'code']);
const emit = defineEmits(['update:visible', 'ok', 'close']);

/**
 * 搜索模块
 */
const tableRef = ref<TableRef>();
const dialogVisible = ref(false);

const { handleFullscreen, getMaxHeight } = useModal();

watch(
    () => dialogVisible.value,
    val => {
        emit('update:visible', val);
    }
);

watch(
    () => props.visible,
    val => {
        dialogVisible.value = val;
    }
);

watch(
    () => tableRef.value?.tableData,
    tableData => {
        if (!props.code) return;
        if (tableData && Object.keys(tableData).length) {
            // 初始化物料编码列表选择行
            const row = tableData.filter(
                (item: CustomerInformationListInfo) => item.customerCode === props.code
            )[0];
            if (row) tableRef.value?.setCurrentRow(row); // 设置表格选中当前行
        }
    }
);

/**
 * 客户信息单选
 */
const handleOk = () => {
    const selectRow = tableRef.value?.selectTableRowData;

    dialogVisible.value = false;

    emit('ok', selectRow || {});
};

const router = useRouter();
const toLink = () => {
    dialogVisible.value = false;
    router.push('/base');
    emit('close');
};
</script>

<template>
    <q-modal
        v-model:visible="dialogVisible"
        title="选择客户"
        width="1000px"
        ok-text="保存"
        :fullscreen="true"
        :style="{ top: '40px' }"
        @ok="handleOk"
        @fullscreen="handleFullscreen"
    >
        <base-search :table-ref="tableRef" :search-config="searchConfig" />
        <base-table
            :id="generateNameToLevel('3')"
            ref="tableRef"
            :tableConfig="tableConfig"
            :getDataFn="params => doGetCustomerInformationList(params, '3')"
            :modal-mode="true"
            :show-setting="false"
            :un-action="true"
            :max-height="getMaxHeight"
        >
            <template #tableHeadStart>
                <q-push-button :is-text="false" @click="toLink" />
            </template>
        </base-table>
    </q-modal>
</template>
